<?php
class Excel extends IController{
    public function Doinsert(){
        $mom = new IModel("users");
        $mom->setData($_POST);
        $res = $mom->add();
        if($res){
            header("Location:/excel/show");
        }else{
            echo "添加失败";
        }
    }
    public function show(){
        $mom = new IModel('goods');
        $data = $mom->query();
        $this->data = $data;
        $this->redirect("show");
    }
    public function export(){
        include_once("/classes/Classes/PHPExcel.php");
        $mom = new IModel('goods');
        $data = $mom->query();
        $mom = new IModel('log');
        $info['admin_ip'] = $_SERVER['REMOTE_ADDR'];
        $info['admin_name'] = $this->user['username'];
        $info['times'] = time();
        $info['data'] = serialize($data);
        $mom->setData($info);
        $res = $mom->add();
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A1', 'ID')
            ->setCellValue('B1', '用户名')
            ->setCellValue('C1', '密码')
            ->setCellValue('D1', '真实姓名')
            ->setCellValue('E1', '工资');
        $i = 2;
        foreach($data as $key => $val){
            $objPHPExcel->getActiveSheet()
                ->setCellValue('A'.$i, $val['id'])
                ->setCellValue('B'.$i, $val['username'])
                ->setCellValue('C'.$i, $val['password'])
                ->setCellValue('D'.$i, $val['realname'])
                ->setCellValue('E'.$i, $val['salary']);
        }
        $objPHPExcel->getActiveSheet()->setTitle('员工工资详情');
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="01simple.xlsx"');
        header('Cache-Control: max-age=0');
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
        $objWriter->save('php://output');

    }
}